﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;

namespace BusinessLayer.RegInfo
{
    public class MenuBL
    {
        public Menu GetById(int id)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", id);

            var list = adapter.GetList<Menu>("sp_Menu_Select_By_Id", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }

        public List<Menu> GetAll()
        {          
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<Menu>("sp_Menu_Select_All", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }

        public void Insert(Menu menu)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@MenuName", menu.MenuName);
            adapter.AddParameter("@Order", menu.Order);
            adapter.AddParameter("@RedirectUrl", menu.RedirectUrl);
            adapter.AddParameter("@Status", menu.Status);
            adapter.ExecuteNonQuery("sp_Menu_Insert", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);            
        }

        public void Update(Menu menu)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", menu.Id);
            adapter.AddParameter("@MenuName", menu.MenuName);
            adapter.AddParameter("@Order", menu.Order);
            adapter.AddParameter("@RedirectUrl", menu.RedirectUrl);
            adapter.AddParameter("@Status", menu.Status);
            adapter.ExecuteNonQuery("sp_Menu_Update", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public void Delete(int id)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", id);
            adapter.ExecuteNonQuery("sp_Menu_Delete", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }
    }
}
